﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
using System.Text.RegularExpressions;
using OgilvyOne.Utility;

namespace OgilvyOne.BLL
{
    /// <summary>
    /// 分页标签
    /// <cms:page value="model方法名,返回值为PageTools" pagesize="分页大小">
    ///     <column>循环列表</column>
    ///     <page>分页</page>
    /// </cms:page>
    /// </summary>
    class PageLabel : CommonLabel
    {
        public PageLabel(CommonModel model)
            : base(model)
        {

        }

        private string pagestr;
        /// <summary>
        /// 分页HTML
        /// </summary>
        public string PageStr
        {
            set { pagestr = value; }
            get { return pagestr; }
        }
        private int pagesize ;
        /// <summary>
        /// 分页大小
        /// </summary>
        public int Pagesize
        {
            get { return pagesize; }
            set { pagesize = value; }
        }


        public override object[] GetMoedthParm()
        {
            if (pagesize > 0) return new object[] { pagesize };
            return null;
        }
        
        public override string GetLabelHtml()
        {
            
            StringBuilder sb = new StringBuilder("");
            PageTools pagetools = (PageTools)Execute();
            MatchCollection mc = regex.Matches(LabelNodeStr);
            if (pagetools.IsDataSet)
            {//表格
                DataSet dataset = pagetools.DataSet;
                DataTable table = dataset.Tables[0];
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    DataRow row = table.Rows[i];
                    string lihtml = LabelNodeStr;
                    foreach (Match m in mc)
                    {
                        string key = GetCmsValueStr(m.Value);
                        lihtml = StringUtil.ReplaceStr(lihtml, m.Value, row[key].ToString());

                    }
                    sb.AppendLine(lihtml);
                }
            }
            else
            {//list
                IList datalist = (IList)pagetools.DataList;
                for (int i = 0; i < datalist.Count; i++)
                {
                    object row = datalist[i];
                    string lihtml = LabelNodeStr;
                    foreach (Match m in mc)
                    {
                        string key = GetCmsValueStr(m.Value);//.Replace("<cms", "").Replace("value=", "").Replace("'", "").Replace("\"", "").Trim();
                        lihtml = StringUtil.ReplaceStr(lihtml, m.Value, row.GetType().InvokeMember(key, BindingFlags.InvokeMethod | BindingFlags.GetProperty, null, row, null).ToString());
                    }
                    sb.AppendLine(lihtml);
                }

            }
            string pagehtml = pagestr;
            mc = regex.Matches(pagestr);
            foreach (Match m in mc)
            {
                string key = GetCmsValueStr(m.Value);
                pagehtml = StringUtil.ReplaceStr(pagehtml, m.Value, pagetools.GetType().InvokeMember(key, BindingFlags.InvokeMethod | BindingFlags.SetProperty, null, pagetools, null).ToString());
            }
            sb.AppendLine(pagehtml);
            return sb.ToString();
        }
    }
}
